package xioc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Logger;

/**
 *
 * @author Gailufeng
 */
public class XmlIoc implements Ioc {

    protected static Logger log = Logger.getLogger(XmlIoc.class.getName());
    private static Map<String, Bean> beansProsMap;
    private static Map<String, Object> beansMap;

    public XmlIoc(String fileName) {
        beansProsMap = XmlIocLoader.parse(fileName);
        beansMap = new HashMap<String, Object>();
        Iterator<Entry<String, Bean>> entryIterator = beansProsMap.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Entry<String, Bean> entryMap = entryIterator.next();
            String beanName = entryMap.getKey();
            Object object = getObject(entryMap.getKey());
            beansMap.put(beanName, object);
            log.info(object.getClass().getName() + " --> " + beanName);
        }
    }

    public Object getBean(String beanId) {
        return beansMap.get(beanId);
    }

    private Object getObject(String name) {
        Bean bean = (Bean) beansProsMap.get(name);
        Object obj = BeanProcesser.newInstance(bean.getType());
        setProperty(bean, obj);
        return obj;
    }

    // 依赖注入
    private void setProperty(Bean bean, Object obj) {

        Map<String, Object> propertiesMap = bean.getProperties();
        Iterator<String> keysIterator = propertiesMap.keySet().iterator();
        while (keysIterator.hasNext()) {
            String property = keysIterator.next();
            Object value = propertiesMap.get(property);

            // 处理 value
            if (value instanceof String) {
                BeanProcesser.setField(obj, property, (String) value);
            }

            // 处理 ref
            if (value instanceof String[]) {
                String[] strsValue = (String[]) value;
                if (strsValue.length == 1) {
                    String beanId = ((String[]) value)[0];
                    BeanProcesser.setField(obj, property, getObject(beanId));
                }
            }

            // 处理 list
            if (value instanceof List) {
                Iterator<?> valuesIterator = ((List<?>) value).iterator();
                List<Object> valuesList = new ArrayList<Object>();
                while (valuesIterator.hasNext()) {
                    Object valueList = (Object) valuesIterator.next();
                    if (valueList instanceof String[]) {
                        valuesList.add(getObject(((String[]) valueList)[0]));
                    }
                }
                BeanProcesser.setField(obj, property, valuesList);
            }

            // 处理 map
            if (value instanceof Map) {
                Iterator<?> entryIterator = ((Map<?, ?>) value).entrySet().iterator();
                Map<Object, Object> map = new HashMap<Object, Object>();
                while (entryIterator.hasNext()) {
                    Entry<?, ?> entryMap = (Entry<?, ?>) entryIterator.next();
                    if (entryMap.getValue() instanceof String[]) {
                        map.put(entryMap.getKey(),
                                getObject(((String[]) entryMap.getValue())[0]));
                    }
                }
                BeanProcesser.setField(obj, property, map);
            }
        }
    }
}
